import { TodoAction } from "../index"

type TodoItem = {
    id: number,
    name: string,
    isDone: boolean
}
export type TodoList = TodoItem[]
const initValue: TodoList = [
    // {
    //   id: 1,
    //   name: '吃饭',
    //   isDone: true,
    // },
    // {
    //   id: 2,
    //   name: '睡觉',
    //   isDone: true,
    // },
    // {
    //   id: 3,
    //   name: '打豆豆',
    //   isDone: false,
    // },
  ]
  export default function todos(state = initValue, action: TodoAction): TodoList {

    console.log(action)

    if(action.type === 'todos/del') {
  
        return state.filter(it=>it.id !== action.payload)
    } else if(action.type === 'todos/add') {
        return [...state, {id: Date.now(), name: action.name, isDone: false}]
    } else if(action.type === 'todos/update') {
        const id = action.id 
        // 更新编号为id 的 isDone
        return state.map(it => {
            if(it.id  === id) {
                return { id: it.id, name: it.name, isDone: !it.isDone}
            } else {
                return it
            }
        })
    } else if(action.type=== 'todos/init') {
        return action.playload
    }
    return state
  }
  